package com.yy.design.pattern.chain.of.responsibility.openaccount;

import com.alibaba.fastjson.JSON;
import com.yy.design.pattern.chain.of.responsibility.AbstractChainHandler;
import com.yy.design.pattern.chain.of.responsibility.ChainHandlerContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.lang.ref.PhantomReference;
import java.lang.ref.ReferenceQueue;
import java.lang.ref.SoftReference;
import java.lang.ref.WeakReference;

@Slf4j
@Component
public class Check1Handler extends AbstractChainHandler {
    @Override
    public void handle(ChainHandlerContext handlerContext) {
        log.info("handler1 context : {}", JSON.toJSONString(handlerContext));
        AccountContext accountContext = (AccountContext) handlerContext;
        log.info("handler1 convert context : {}", JSON.toJSONString(accountContext));
        int a = 1 / 0;

        invokeNextHandler(handlerContext);

        SoftReference<Object> softReference = new SoftReference<>(new Object());
        Object obj = softReference.get();
        // 需要判断是否为空，因为软引用在内存不足时会被回收
        if (obj != null) {
            // do something
        }

        WeakReference<Object> weakReference = new WeakReference<>(new Object());

        PhantomReference<Object> phantomReference = new PhantomReference<>(new Object(), new ReferenceQueue<>());
    }
}
